﻿/*
 * Module Name: AES.cs
 * 
 * Purpose: This is used for the AES algorithm
 * 
 * Created by: Chris Tisdale
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Common_Code.Encryptions
{
    class AES : Encryption
    {
        /*
         * These are the private variables used throught the function
         */
        private byte[] pszKey;
        private byte[] pszText;
        private byte[] IV;

        /*
         * Name: AES
         * 
         * Parameters: byte[] pszText
         *             byte[] pszKey
         *             byte[] IV
         * 
         * Returns: An AES Object
         * 
         * Purpose: Creates a AES encryption object
         */
        public AES(byte[] pszText, byte[] pszKey, byte[] IV)
        {
            this.pszText = pszText;
            this.pszKey = pszKey;
            this.IV = IV;
        }

        /*
         * Name: ~AES
         * 
         * Parameters: None
         * 
         * Returns: None
         * 
         * Purpose: Clears the information in memory
         */
        ~AES()
        {
            Array.Clear(pszText, 0, pszText.Length);
            Array.Clear(pszKey, 0, pszKey.Length);
            Array.Clear(IV, 0, IV.Length);
        }

        /*
         * Name: Encrypt
         * 
         * Parameters: NA
         * 
         * Returns: byte[]
         * 
         * Purpose: This function is used to Encrypt text
         */
        public byte[] Encrypt()
        {
            // Check arguments.
            if (pszText == null || pszText.Length <= 0)
                throw new ArgumentNullException("pszText");
            if (pszKey == null || pszKey.Length <= 0)
                throw new ArgumentNullException("pszKey");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (var rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = pszKey;
                rijAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(pszText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }
            // Return the encrypted bytes from the memory stream.
            return encrypted;
        }

        /*
         * Name: SetKey
         * 
         * Parameters: byte[] pszKey
         * 
         * Returns: None
         * 
         * Purpose: This function sets the key for the encryption algorithm
         */
        public void SetKey(byte[] pszKey)
        {
            this.pszKey = pszKey;
        }

        /*
         * Name: SetIV
         * 
         * Parameters: byte[] IV
         * 
         * Returns: None
         * 
         * Purpose: This function sets the IV for the encryption algorithm
         */
        public void SetIV(byte[] IV)
        {
            this.IV = IV;
        }

        /*
         * Name: SetText
         * 
         * Parameters: byte[] pszText
         * 
         * Returns: None
         * 
         * Purpose: This function sets the text for the encryption algorithm
         */
        public void SetText(byte[] pszText)
        {
            this.pszText = pszText;
        }

        /*
         * Name: Decrypt
         * 
         * Parameters: NA
         * 
         * Returns: byte[]
         * 
         * Purpose: This function is used to Decrypt text that is already Encrypted
         */
        public byte[] Decrypt()
        {
            // Check arguments.
            if (pszKey == null || pszText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (pszKey == null || pszKey.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (var rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = pszKey;
                rijAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (var msDecrypt = new MemoryStream(pszText))
                {
                    using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (var srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }
            return System.Text.Encoding.ASCII.GetBytes(plaintext);
        }
    }
}
